package com.hawk.eye.server.convert;

import com.hawk.eye.server.domain.dto.req.ProblemRecordReqDTO;
import com.hawk.eye.server.domain.dto.resp.ProblemRecordRespDTO;
import com.hawk.eye.server.domain.dto.resp.RepairProblemRespDTO;
import com.hawk.eye.api.storage.dto.ServiceLogRespDTO;
import com.hawk.eye.server.domain.po.ProblemRecord;
import com.hawk.eye.server.domain.po.ServiceConfig;
import com.hawk.eye.server.domain.vo.req.ProblemRecordReqVO;
import com.hawk.eye.server.domain.vo.resp.ProblemRecordRespVO;
import com.hawk.eye.server.domain.vo.resp.RepairProblemRespVO;
import org.mapstruct.DecoratedWith;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

/**
 * @author zd
 */
@Mapper
@DecoratedWith(ServiceProblemConvert.ServiceProblemConvertDecorator.class)
public interface ServiceProblemConvert {

    ServiceProblemConvert INSTANCE = Mappers.getMapper(ServiceProblemConvert.class);

    ProblemRecordReqDTO to(ProblemRecordReqVO reqVO);

    RepairProblemRespVO to(RepairProblemRespDTO reqVO);

    @Mapping(source = "po.id", target = "id")
    @Mapping(source = "po.baseAlarmId", target = "baseAlarmId")
    @Mapping(source = "po.traceId", target = "traceId")
    @Mapping(source = "serviceConfig.serviceCnName", target = "serviceCnName")
    @Mapping(source = "serviceConfig.serviceZnName", target = "serviceZnName")
    @Mapping(source = "po.alarmTime", target = "alarmTime")
    @Mapping(source = "po.stackHash", target = "stackHash")
    ProblemRecordRespDTO to(ProblemRecord po, ServiceLogRespDTO serviceLogRespDTO, ServiceConfig serviceConfig);

    ProblemRecord to(ProblemRecordReqDTO reqDTO);

    ProblemRecordRespVO to(ProblemRecordRespDTO respDTO);

    public abstract class ServiceProblemConvertDecorator implements ServiceProblemConvert {


        @Override
        public ProblemRecordRespDTO to(ProblemRecord po, ServiceLogRespDTO serviceLogRespDTO, ServiceConfig serviceConfig) {
            ProblemRecordRespDTO problemRecordRespDTO = new ProblemRecordRespDTO();
            if (po != null) {
                problemRecordRespDTO.setId(po.getId());
                problemRecordRespDTO.setBaseAlarmId(po.getBaseAlarmId());
                problemRecordRespDTO.setDisposeUser(po.getDisposeUser());
                problemRecordRespDTO.setProblemDesc(po.getProblemDesc());
                problemRecordRespDTO.setProblemCause(po.getProblemCause());
                problemRecordRespDTO.setResolveDesc(po.getResolveDesc());
                problemRecordRespDTO.setRecordTime(po.getRecordTime());
                problemRecordRespDTO.setProblemType(po.getProblemType());
                problemRecordRespDTO.setStatus(po.getStatus());
                problemRecordRespDTO.setProblemLevel(po.getProblemLevel());
                problemRecordRespDTO.setTraceId(po.getTraceId());
                problemRecordRespDTO.setProblemName(po.getProblemName());
                problemRecordRespDTO.setResolveTime(po.getResolveTime());
            }
            if (serviceLogRespDTO != null) {
                problemRecordRespDTO.setRequestUri(serviceLogRespDTO.getRequestUri());
                problemRecordRespDTO.setAlarmTime(serviceLogRespDTO.getAlarmTime());
                problemRecordRespDTO.setErrorFileName(serviceLogRespDTO.getErrorFileName());
                problemRecordRespDTO.setStackHash(serviceLogRespDTO.getStackHash());
                problemRecordRespDTO.setChargeUserName(serviceLogRespDTO.getChargeUserName());
                problemRecordRespDTO.setLogMessage(serviceLogRespDTO.getLogMessage());
            }
            if (serviceConfig != null) {
                problemRecordRespDTO.setServiceZnName(serviceConfig.getServiceZnName());
                problemRecordRespDTO.setServiceCnName(serviceConfig.getServiceCnName());
            }
            return problemRecordRespDTO;
        }

    }

}